import 'package:get/get.dart';
import 'package:video_player/video_player.dart';
import '../model/community_video_model.dart';

class TopicDetailListViewController extends GetxController {
  final IssueList item;
  late final VideoPlayerController videoController;

  var isExpanded = false.obs;
  var isPlaying = false.obs;
  var videoInitialized = false.obs;

  TopicDetailListViewController(this.item);

  @override
  void onInit() {
    super.onInit();
    _initializeVideo();
  }

  Future<void> _initializeVideo() async {
    var videoUrl = item.data?.content?.data.playUrl ?? '';
    videoController = VideoPlayerController.networkUrl(Uri.parse(videoUrl))
      ..initialize().then((_) {
        videoInitialized.value = true;
      });
  }

  Future<void> stopVideo() async {
    if (isPlaying.value) {
      await videoController.pause();
      isPlaying.value = false;
    }
  }

  Future<void> playVideo() async {
    if (videoInitialized.value) {
      await videoController.play();
      isPlaying.value = true;
    }
  }

  void onTogglePlayPauseVideo() async {
    if (isPlaying.value) {
      await stopVideo();
    } else {
      await playVideo();
    }
  }

  void expandText() {
    isExpanded.value = true;
  }

  @override
  void onClose() {
    videoController.dispose();
    super.onClose();
  }
}
